document.addEventListener('DOMContentLoaded', () => {
    const loginForm = document.getElementById('loginForm');

    loginForm.addEventListener('submit', async (e) => {
        e.preventDefault();

        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        try {
            const response = await fetch('http://192.168.8.128:8080/admin/employee/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username,
                    password
                })
            });

            const data = await response.json();

            if (data.code === 1) {
                // 存储token和用户信息
                localStorage.setItem('token', data.data.token);
                localStorage.setItem('user', JSON.stringify({
                    id: data.data.id,
                    name: data.data.name,
                    username: data.data.userName
                }));

                // 跳转到主页面
                window.location.href = 'dashboard.html';
            } else {
                alert('登录失败: ' + (data.msg || '用户名或密码错误'));
            }
        } catch (error) {
            console.error('登录错误:', error);
            alert('登录失败，请检查网络连接');
        }
    });
});